<script language="JavaScript1.2" src="../../commons/js/editing.js"></script>
<?
require_once "../../commons/includes/stringutils.inc";


function removeCrlf($text,$subst){
	$value = str_replace(chr(10),"",$text );
	$value = str_replace(chr(13),"$subst",$value );
	
	return $value;
}

function jsNormalizeValue($tmpValue,$crlfReplaceChar='\\n'){
	$escape_chars = "\\" . '\"'."\'"  ;
	$tmpValue = addcslashes($tmpValue,$escape_chars);			
	$tmpValue = str_replace(chr(10),"",$tmpValue );
	$tmpValue = str_replace(chr(13),$crlfReplaceChar,$tmpValue );	
	return	$tmpValue;
}
			
function writeJsValues(&$values,$tableName,$form="form_edit"){
	//@to be remade
	$dbdef = &$GLOBALS['dbdef'];
	$tabledef = &$dbdef [$tableName];
	echo"
	<script type='text/javascript'>
	<!--	
	";
	
	foreach($values as $key=>$value){
		
		if($key=="style-/"){}
		else if($key=="PHPSESSID"){}
		else{
			$type = $tabledef[$key]["DataType"];
			$imode = $tabledef[$key]["InputMode"];
			
			$value = jsNormalizeValue($values[$key]);
			
			if($type=='datetime') {
				$value = StringUtils::stringToDateString($value);	
			}
			
			if("$imode"!="none")
				echo "	try{document.$form.$key.value = '$value'}catch(e){};\n" ;
			
		}	
	}
	
	echo "
	-->
	</script> 
	";
}	

function showJsValues(&$values,&$tableName,$form="form_edit"){
	$dbdef = &$GLOBALS['dbdef'];
	$tabledef = &$dbdef [$tableName];
	echo"
	<script type='text/javascript'>
	<!--	
	";
	
	foreach($values as $key=>$value){
		
		if($key=="style-/"){}
		else if($key=="PHPSESSID"){}
		else{
			
			$type = $tabledef[$key]["DataType"];
			
			
			
			if($type=='datetime') {
				$value = StringUtils::stringToDateString($value);	
				$viewValue = $value;
			}else{
				$value = jsNormalizeValue($values[$key]);		
				$viewValue = jsNormalizeValue($values[$key],"<br>");
			}
			
			echo "	
				
				try{if(document.$form.$key) document.$form.$key.value = '$value'}catch(e){};
				el = document.getElementById('Span_$key');
				if(el) el.innerHTML = '$viewValue';
			" ;
		}	
	}
	
	echo "
	-->
	</script> 
	";
}	

/*
	Optional parameters.
	
	AddEmptyOption :
		Y not set : empty option added in combo box
		N 	 	: no empty option added in combo box
		
	LabelStyle :
		label Style without style keyword es: 'width:30pt';
	
	InputStyle
		Input box  Style without style keyword es: 'width:30pt';
		
	AddHiddenField
		add a input hidden in view mode
	
	NoLabel	
		Hide label
	
	Value	
		Input tag Value attribute
	
	CallBack	
		CallBack function
		
*/

function viewHtml($tableName,$pFieldName,$label="",$paraOptions=""){
	$dbdef = &$GLOBALS['dbdef'];
	
	$tabledef = &$dbdef [$tableName];
	
	$inputMode = $tabledef[$pFieldName]["InputMode"];
	if("$inputMode"=="") $inputMode="hidden";
	
	if($inputMode=="hidden") $function = "hidden_text";	
	else $function = "view_text_text";	
	
	if("$label"=="") $label=$tabledef[$pFieldName]["DisplayLabel"];				
	$size = $tabledef[$pFieldName]["DisplaySize"]*3.94;
	
	$maxlength= $tabledef[$pFieldName]["DataLength"];
	$inputSpec = $tabledef[$pFieldName]["InputSpec"];
	
	if($size==0) $size = $maxlength;
	$caller="normal";
	
	return $function($pFieldName,$label,$size,$maxlength,$inputSpec,$caller,$paraOptions);
}

function inputHtml($tableName,$pFieldName,$label="",$paraOptions){
	$dbdef = &$GLOBALS['dbdef'];
	
	$tabledef = &$dbdef [$tableName];
	
	$inputMode = $tabledef[$pFieldName]["InputMode"];
	if("$inputMode"=="") $inputMode="hidden";
	
	if($inputMode=="hidden") $function = "hidden_text";
	else $function = $tabledef[$pFieldName]["InputMode"] . "_". $tabledef[$pFieldName]["DataType"] ;
		
	if("$label"=="") $label=$tabledef[$pFieldName]["DisplayLabel"];	

	$size = $tabledef[$pFieldName]["DisplaySize"]*3.94;
	$maxlength= $tabledef[$pFieldName]["DataLength"];
	$inputSpec = $tabledef[$pFieldName]["InputSpec"];
	
	if("$maxlength"=="" || "$maxlength"=="0") $maxlength = "10";
	if($size==0) $size = $maxlength*3.94;
	$caller="normal";
	return $function($pFieldName,$label,$size,$maxlength,$inputSpec,$caller,$paraOptions);
}

function inputSearchHtml($tableName,$pFieldName,$label="",$paraOptions){
	$dbdef = &$GLOBALS['dbdef'];
	
	$tabledef = &$dbdef [$tableName];
	
	$inputMode = $tabledef[$pFieldName]["InputMode"];
	if("$inputMode"=="") $inputMode="hidden";
	
	if($inputMode=="hidden") $function = "hidden_text";
		
	if("$label"=="") $label=$tabledef[$pFieldName]["DisplayLabel"];				
	
	$maxlength= $tabledef[$pFieldName]["DataLength"];
	$inputSpec = $tabledef[$pFieldName]["SelectSpec"];
	
	$size = "140";
		
	$prefix="vc__";
	if($dataType=='numeric') $prefix="in__";
	else $prefix="vc__";
	
	$function="search_combo_text";	
	$caller="search";
	
	$pFieldName = $prefix.$pFieldName;
	
	return $function($pFieldName,$label,$size,$maxlength,$inputSpec,$caller,$paraOptions);
}

function setLabelStyle(&$paraOptions){
	if(isset($paraOptions["NoLabel"])){
		return "style='display:none;'";
	}
	
	if(isset($paraOptions["LabelStyle"]))  return "style='" . $paraOptions["LabelStyle"] . "' ";
	else return "";
}

function setInputStyle(&$paraOptions,$default){
	if(isset($paraOptions["InputStyle"]))  return "style='" . $paraOptions["InputStyle"] . "' ";
	else return "style='" . $default . "' ";
}

function setCallBack(&$paraOptions){
	if(isset($paraOptions["CallBack"]))  return $paraOptions["CallBack"];
	else return "";
}

function setInputValueAttribute(&$paraOptions){
	$pValue = $paraOptions["InputValue"];
	if("$pValue"=="") return "";
	else return "value='$pValue'";
}

function setComboEmptyOption(&$paraOptions){
	if($paraOptions["AddEmptyOption"]=="N")   return "";
	else return "<option value=''>";
}

function setAddHiddenField(&$paraOptions,$pFieldName){
	if(isset($paraOptions["AddHiddenField"]))  return "\n 		<input type='hidden' name='$pFieldName'>";
	else return "";
}
	
function view_text_text($pFieldName,$label,$size,$maxlength,$inputSpec,$caller,&$paraOptions) {
	
	$labelStyle=setLabelStyle($paraOptions);
	$hiddenField = setAddHiddenField($paraOptions,$pFieldName);
	
	return  "
			$hiddenField
			<label class='view' title='$label' $labelStyle>$label</label>
			<span class='view' id='Span_$pFieldName'></span>
		";
}


function hidden_text($pFieldName,$label,$size,$maxlength,$inputSpec,$caller,&$paraOptions) {
	return  "<input type='hidden' id='$pFieldName' name='$pFieldName' ><!-- hidden_text -->";
}

function search_combo_text($pFieldName,$label,$size,$maxlength,$inputSpec,$caller,&$paraOptions) {
	ob_start();
	include $inputSpec;
	$options = ob_get_contents();
	ob_end_clean();
	
	$pValue = setInputValueAttribute($paraOptions);
	$labelStyle=setLabelStyle($paraOptions);	
	$inputStyle=setInputStyle($paraOptions,"width:{$size}pt;");
	
	$tmp = "	<label title='$label' $labelStyle>$label</label>
				<input id='$pFieldName' $pValue name='$pFieldName' $inputStyle maxlength='$maxlength'>
			";	
			
	if($inputSpec=="") return $tmp;
	
	$tmp.= "	<select id='L$pFieldName' style='width:200px' name='L$pFieldName' onchange=\"el = document.getElementById('$pFieldName'); if(this.value=='*'){tmpvalue='';}else{tmpvalue=el.value;if(tmpvalue=='')tmpvalue=this.value; else tmpvalue+=','+this.value} el.value=tmpvalue;this.value='';\">
				<option value=''>-- Select --
				$options
				<option value='*'>No filter
				</select><!-- combo_text -->
			";
	return $tmp;		

}

function combo_text($pFieldName,$label,$size,$maxlength,$inputSpec,$caller,&$paraOptions) {

	ob_start();
	include $inputSpec;
	$options = ob_get_contents();
	ob_end_clean();
   
	$empty = setComboEmptyOption($paraOptions);
	$labelStyle=setLabelStyle($paraOptions);
	
	return  "	<label title='$label' $labelStyle>$label</label>
					<select id='$pFieldName' name='$pFieldName'>\n
					$empty	
					$options
					</select><!-- combo_text -->";
}

function date_datetime($pFieldName,$label,$size,$maxlength,$inputSpec,$caller,&$paraOptions) {
	$size ="50";
	$labelStyle=setLabelStyle($paraOptions);
	$inputStyle=setInputStyle($paraOptions,"width:{$size}pt;");
	
	if (!empty($paraOptions)) {
		$callBack = "gCallbackfn='opener.".setCallBack($paraOptions)."()';";
	}

	return  "
			<label title='$label' $labelStyle>$label</label>
			<input onBlur='sepDate(this);' onKeyUp='return autoTab(this, 8, event);' onKeypress='return checkdate(event, this)'id='$pFieldName' name='$pFieldName' $inputStyle maxlength='10'>
			<a href=\"javascript:{$callBack}show_calendar('document.all.$pFieldName', document.all.$pFieldName.value);\">
				&#133;
			</a>

			";
}

function yesno_text($pFieldName,$label,$size,$maxlength,$inputSpec,$caller,&$paraOptions) {
	
	$labelStyle=setLabelStyle($paraOptions);
	if(file_exists($inputSpec)){
		ob_start();
		include $inputSpec;
		$options = ob_get_contents();
		ob_end_clean();
	}	
	else
		$options="
			<option value='Yes'>Yes
			<option value='No'>No
		";
	return  "
			<label title='$label' $labelStyle>$label</label>
			<select id='$pFieldName' name='$pFieldName'>
				$options	
			</select>
		";
}

function number_numeric($pFieldName,$label,$size,$maxlength,$inputSpec,$caller,&$paraOptions) {
	//@@@@@ da sistemare
	$labelStyle=setLabelStyle($paraOptions);
	$inputStyle=setInputStyle($paraOptions,"width:{$size}pt;");
	
	return  "
			<label title='$label' $labelStyle>$label</label>
			<input onKeyPress='return checkint(event, this)' id='$pFieldName' name='$pFieldName' $inputStyle maxlength='$maxlength'><!-- text_text -->
			";
}

function currency_numeric($pFieldName,$label,$size,$maxlength,$inputSpec,$caller,&$paraOptions) {
	//@@@@@ da sistemare
	$labelStyle=setLabelStyle($paraOptions);
	$inputStyle=setInputStyle($paraOptions,"width:{$size}pt;");
	return  "
			<label title='$label' $labelStyle>$label</label>
			<input onKeyPress='return checkdecimal(event, this)' id='$pFieldName' name='$pFieldName' $inputStyle maxlength='$maxlength'><!-- text_text -->";
}

function textarealocked_text($pFieldName,$label,$size,$maxlength,$inputSpec,$caller,&$paraOptions) {
	//@@@@@ da sistemare
	$labelStyle=setLabelStyle($paraOptions);
	
	$size="100";
	$inputStyle=setInputStyle($paraOptions,"width:{$size}pt;background-color:transparent;border:0;");
	
	return  "<label title='$label' $labelStyle>$label</label>
			 <input id='$pFieldName' name='$pFieldName' $inputStyle maxlength='$maxlength'><!-- text_text -->";
}

function textlocked_text($pFieldName,$label,$size,$maxlength,$inputSpec,$caller,&$paraOptions) {
	$labelStyle=setLabelStyle($paraOptions);
	$inputStyle=setInputStyle($paraOptions,"width:{$size}pt;background-color:transparent;border:0;");
	
	return  "<label title='$label' $labelStyle>$label</label>
			 <input readonly tabindex='-1' id='$pFieldName' name='$pFieldName' $inputStyle maxlength='$maxlength' ><!-- text_text -->";
}

function numberlocked_numeric($pFieldName,$label,$size,$maxlength,$inputSpec,$caller,&$paraOptions) {
	$labelStyle=setLabelStyle($paraOptions);
	$inputStyle=setInputStyle($paraOptions,"width:{$size}pt;text-align:right;background-color:transparent;border:0;");
	
	return  "<label title='$label' $labelStyle>$label</label>
			 <input readonly tabindex='-1' id='$pFieldName' name='$pFieldName' $inputStyle maxlength='$maxlength' ><!-- text_text -->";
}

function textlocked_datetime($pFieldName,$label,$size,$maxlength,$inputSpec,$caller,&$paraOptions) {
	$labelStyle=setLabelStyle($paraOptions);
	$inputStyle=setInputStyle($paraOptions,"width:{$size}pt;background-color:transparent;border:0;");
	
	return  "<label title='$label' $labelStyle>$label</label>
			 <input readonly tabindex='-1' id='$pFieldName' name='$pFieldName' $inputStyle maxlength='$maxlength' ><!-- text_text -->";
}

function text_text($pFieldName,$label,$size,$maxlength,$inputSpec,$caller,&$paraOptions) {
	$labelStyle=setLabelStyle($paraOptions);
	$inputStyle=setInputStyle($paraOptions,"width:{$size}pt;");
	
	return  "<label title='$label' $labelStyle>$label</label>
			 <input id='$pFieldName' name='$pFieldName' $inputStyle maxlength='$maxlength'><!-- text_text -->";
}

function textarea_fulltext($pFieldName,$label,$size,$maxlength,$inputSpec,$caller,&$paraOptions) {
	$labelStyle=setLabelStyle($paraOptions);
	$inputStyle=setInputStyle($paraOptions,"width:{$size}pt;");
	
	return  "<label title='$label' $labelStyle>$label</label>
			 <textarea id='$pFieldName' name='$pFieldName' $inputStyle maxlength='$maxlength' rows='3'></textarea><!-- textarea_text -->";
}

function textarea_text($pFieldName,$label,$size,$maxlength,$inputSpec,$caller,&$paraOptions) {
	$labelStyle=setLabelStyle($paraOptions);
	$inputStyle=setInputStyle($paraOptions,"width:{$size}pt;");
	
	return  "<label title='$label' $labelStyle>$label</label>
			 <textarea id='$pFieldName' name='$pFieldName' $inputStyle maxlength='$maxlength' rows='3'></textarea><!-- textarea_text -->";
}

function textcombo_text($pFieldName,$label,$size,$maxlength,$inputSpec,$caller,&$paraOptions) {
	
		
	ob_start();
	include $inputSpec;
	$options = ob_get_contents();
	ob_end_clean();
	
	$empty = setComboEmptyOption($paraOptions);
	$labelStyle=setLabelStyle($paraOptions);
	$inputStyle=setInputStyle($paraOptions,"width:{$size}pt;");
	
	return  "	<label title='$label' $labelStyle>$label</label>
					<input id='$pFieldName' name='$pFieldName' $inputStyle maxlength='$maxlength'>
					<select style='width:150pt' id='L$pFieldName' name='L$pFieldName' onchange=\"document.getElementById('$pFieldName').value=this.value;this.value='';\">
					$empty
					$options
					</select><!-- text_combo -->
				";
}

function textareacombo_text($pFieldName,$label,$size,$maxlength,$inputSpec,$caller,&$paraOptions) {
	ob_start();
	include $inputSpec;
	$options = ob_get_contents();
	ob_end_clean();
	
	$empty = setComboEmptyOption($paraOptions);
	$labelStyle=setLabelStyle($paraOptions);
	$inputStyle=setInputStyle($paraOptions,"width:{$size}pt;");
	
	return  "	<label title='$label' $labelStyle>$label</label>
					<textarea id='$pFieldName' name='$pFieldName' $inputStyle maxlength='$maxlength' rows='3'></textarea>
					<select style='width:150pt' id='L$pFieldName' name='L$pFieldName' onchange=\"document.getElementById('$pFieldName').value=this.value;this.value='';\">
					$empty
					$options
					</select><!-- textareacombo_text -->
				";
}


////////////////////////////////////////////////*******************************
function lcombo_text($pFieldName,$label,$size,$maxlength,$inputSpec,$caller,&$paraOptions) {


	ob_start();
	include $inputSpec;
	$obout = ob_get_contents();
	ob_end_clean();
	$rf = split("\n", $obout);
	$options = '';
	foreach($rf as $r){
		$r = str_replace(chr(10),'',$r);
		$r = str_replace(chr(13),'',$r);
		$sp = split("\t", $r);
		if (count($sp)==2) {
			$options .= '<option value="'.$sp[0].'">'.$sp[1]."\n";
		}
	}
	
	$empty = setComboEmptyOption($paraOptions);
	$labelStyle=setLabelStyle($paraOptions);
	
	return  "	<label title='$label' $labelStyle>$label</label>
					<select id='$pFieldName' name='$pFieldName'>\n
					$empty	
					$options
					</select><!-- combo_text -->";

}

function textToOptions($inputSpec) {

	ob_start();
	include $inputSpec;
	$obout = ob_get_contents();
	ob_end_clean();
	
	$rf = split("\n", $obout);
	$options = '';
	foreach($rf as $r){
		$r = str_replace(chr(10),'',$r);
		$r = str_replace(chr(13),'',$r);
		$sp = split("\t", $r);
		if (count($sp)==2) {
			$options .= '<option value="'.$sp[0].'">'.$sp[1]."</options>";
		}
	}
	
	return  $options;

}


?>